<?php
namespace app\index\controller;

use app\core\Outcontroller;
use Qiniu\Http\Request;
use think\Db;

class Index extends Outcontroller
{
    public function _empty($name){
        return $name;
    }

    public function index(){ //首页
        //时装标签
        $type = model('type');
        $type = $type->field('name,en_name,thumb')->where('user_id=0')->limit(0,5)->select();
        $this->assign('type',$type);

        //时装列表
        $shop = model('shop');
        $shop = $shop->field('id,title,thumb,like_num,fav_num,click_num')->limit(0,20)->order('id desc')->select();
        $this->assign('shop',$shop);

        //文章列表
        $article = model('article');
        $article = $article->field('id,title,description,thumb,create_time')->limit(0,5)->order('id desc')->select();
        $this->assign('article',$article);

        //查询友情链接
        $link = model('link');
        $link = $link->field('name,thumb,url')->select();
        $link_img = [];
        $link_text = [];
        foreach ($link as $val){
            if($val['thumb']){
                $link_img[] = $val;
            }else{
                $link_text[] = $val;
            }

        }
        $this->assign('link_img'  , $link_img);
        $this->assign('link_text' , $link_text);

        $this->show('index/index');
    }

    public function type(){  //分类
        $type_name = request()->param('type');
        $do_type   = request()->param('c_type');
        $do_flag   = request()->param('flag');
        //查询分类是否存在
        $type = model('type');
        $type = $type->field('id,name,en_name,p_en_name,thumb,show_thumb,description')->whereOr(['en_name'=>$type_name,'p_en_name'=>$type_name])->select();

        if(!$type){
            $this->redirect('index/index/index');
        }

        $p_type = [];
        $c_type = [];
        foreach ($type as $val){
            if(!$val->p_en_name){
                $p_type = $val;
                continue;
            }
            $c_type[] = $val;
        }

        $flag = model('flag');
        $flag = $flag->field('name,en_name')->where(['row_status'=>0])->select();

        $shop = model('shop');
        $shop_res = $shop->field('id,thumb,title');
        if($do_type){
            $shop_res = $shop_res->whereLike('type_id','%,'.$do_type.',%');
        }
        if($do_flag){
            $shop_res = $shop_res->whereLike('flag_id','%,'.$do_flag.',%');
        }
        $shop_res = $shop_res->where(['row_status'=>0])->order('id desc')->limit(0,50)->select();

        //dump($shop::getLastSql());
        //dump($shop_res);exit;

        $this->assign('website',[
            'title' => '-'.$p_type->name
        ]);
        $this->assign('type',$p_type);
        $this->assign('c_type',$c_type);
        $this->assign('do_type',$do_type);
        $this->assign('flag',$flag);
        $this->assign('do_flag',$do_flag);
        $this->assign('shop',$shop_res);
        $this->show('index/type');
    }

    /**
     * 搭配详情
     */
    public function works(){
        $id = (int)request()->param('id');
        if(!$id){
            $this->redirect('index/index');
        }

        $shop = model('shop');
        $shop_res = $shop->where(['id'=>$id])->find();
        if(!$shop_res->id){
            $this->redirect('index/index');
        }

        //统计点击量
        $update = $shop_res->save(['click_num'=>$shop_res->click_num+1],['id'=>$shop_res->id]);
        $shop->saveLog();

        $this->assign('shop',$shop_res);
        $this->show('index/works');
    }

    /**
     * 搭配文章
     *
     */
    public function article(){
        $id = intval(request()->param('id'));
        if(!$id){
            $this->redirect('index/index');
        }

        $article = model('article');
        $article_res = $article->find(['id'=>$id]);
        if(!$article_res->id){
            $this->redirect('index/index');
        }

        //统计点击量
        $update = $article->save(['click_num'=>$article_res->click_num+1],['id'=>$article_res->id]);

        $this->assign('article',$article_res);
        $this->show('index/article');

    }
}
